export class Decoder {

    constructor() {
        this.V = 0
        this.res=  [];
    }

     decode(text) {
        this.res = [];
        this.V = 0;
        let h = text.indexOf("\n\n");
        if (h === -1) {
            // console.log(text);
            throw new Error("error ");
        }
        this.V = h + 2;
        let f = text;
        while (h !== 0) {
            h = this.parseBody(f);
        }
        return this.res;
    }

     parseBody(f) {
        let c = this.V;
        if (c === f.length) {
            return 0;
        }
        let d = f.indexOf("\n", c);
        if (d === -1) {
            return 1;
        }
        if (c === d) {
            return 2;
        }
        let e = f.substring(c, d);
        let i = parseInt(e, 10);
        if (d + i > f.length) {
            return 1;
        }
        let str2 = f.substring(d+1, i + d);
        this.res.push(JSON.parse(str2));
        this.V = d + i;
        if (this.V === f.length) {
            return 3;
        }
        return 1;
    }
}


export function parse(text){
    let res = [];
    let l = text.indexOf('\n\n');
    text = text.substring(l+2,)
    l = text.indexOf('\n');
    while (l != -1){
        let next_len = parseInt(text.substring(0,l));
        if (Number.isNaN(next_len)){
            text = text.substring(l,)
            continue
        }
        let next_str = text.substring(l+1,next_len + l)
        try {
            res.push(JSON.parse(next_str))
        }catch (e){
            debugger
        }
        text = text.substring(l + next_len)
        l = text.indexOf('\n');
    }
    return res;

}

// let res = parse(`)]}'
//
// 376
// [["wrb.fr","H028ib","[[[[3,\\"Saskatoon, Saskatchewan\\",\\"Saskatoon\\",\\"City in Saskatchewan, Canada\\",\\"/m/018d5b\\",null,null,null,null,null,null,3,0.7219025492668152],[[[1,\\"Saskatoon International Airport\\",\\"Saskatoon\\",null,\\"/m/018d5b\\",\\"YXE\\",null,null,\\"YXE\\",null,null,1],\\"2 km\\"]]]]]",null,null,null,"generic"],["di",47],["af.httprm",47,"-7192698419000488840",1]]
// 25
// [["e",4,null,null,412]]
// `)
// let dataList = [];
//
// for (let i = 0; i < res.length; i++) {
//     let a = res[0];
//     // 不是这个 H028ib 的就不处理
//     if (a[0][0] !== "wrb.fr" && a[0][1] !== "H028ib") {
//         continue;
//     }
//     let executeInfo = JSON.parse(a[0][2]);
//     let infoList = executeInfo[0];
//     for (let j = 0; j < infoList.length; j++) {
//         let respExecutedData ={
//             cityMap: {},
//         };
//
//         let infoJ = infoList[j];
//
//         let info = infoJ[0];
//         let type = info[0];
//         respExecutedData.type = type;
//         if (infoJ.length > 1) {
//             let list = infoJ[1];
//
//             let airports = [];
//             for (let k = 0; k < list.length; k++) {
//                 let air = list[k][0];
//                 if (air[0] === 1) {
//                     airports.push(air[5]);
//                     respExecutedData.cityMap[air[5]] = air[4];
//                 }
//             }
//             respExecutedData.infolits = airports;
//         }
//
//         respExecutedData.text = info[4];
//         respExecutedData.code = info[5];
//         respExecutedData.desc = info[1];
//         dataList.push(respExecutedData);
//     }
// }
// console.log(dataList)
